﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GFMS.MODEL;
using KPIS.GERP.GFMS.IDAL;

namespace KPIS.GERP.GFMS.DAL
{
    public class D_BGM_MAS_Expense : IDAL_BGM_MAS_Expense
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
        ConvertNullable cn = new ConvertNullable();

        public IList<M_BGM_MAS_Expense> GetFull(Nullable<int> bgm_exp_grp_seq, Nullable<int> bgm_source_seq
            , bool bLeafOnly)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_BGM_MAS_Expense> infoList = new List<M_BGM_MAS_Expense>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@bgm_exp_grp_seq", bgm_exp_grp_seq);
                dbManager.AddParameters(2, "@bgm_source_seq", bgm_source_seq);

                strSQL = "SELECT exp.bgm_exp_seq, exp.bgm_exp_code, exp.bgm_exp_name"
                    + ", exp.bgm_exp_descr, exp.upper_bgm_exp_seq"
                    + ", exp.bgm_exp_grp_seq, expg.bgm_exp_grp_name"
                    + ", exp.record_status, coalesce(child.child_count, 0) as child_count"
                    + " FROM BgmMasBudgetExpense exp"
                    + " INNER JOIN bgm_mas_expense_source exps ON exp.bgm_exp_seq = exps.bgm_exp_seq"
                        + " AND exps.bgm_source_seq = @bgm_source_seq"
                        + " AND exps.record_status not like @delete_status"
                    + " LEFT JOIN bgm_mas_expense_group expg ON exp.bgm_exp_grp_seq = expg.bgm_exp_grp_seq"
                    + " LEFT JOIN (SELECT upper_bgm_exp_seq, count(*) as child_count"
                        + " FROM bgm_mas_expense"
                        + " WHERE record_status not like @delete_status"
                        + " GROUP BY upper_bgm_exp_seq"
                        + ") child on exp.bgm_exp_seq = child.upper_bgm_exp_seq"
                    + " WHERE exp.record_status not like @delete_status"
                    + (bgm_exp_grp_seq != null ? " and exp.bgm_exp_grp_seq = @bgm_exp_grp_seq" : "")
                    + (bgm_source_seq != null ? " and exps.bgm_source_seq = @bgm_source_seq" : "")
                    + (bLeafOnly ? " and coalesce(child.child_count, 0) = 0" : "");

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_BGM_MAS_Expense info = new M_BGM_MAS_Expense();
                        info.bgm_exp_seq = Convert.ToInt32(drc[i]["bgm_exp_seq"]);
                        info.bgm_exp_code = drc[i]["bgm_exp_code"].ToString();
                        info.bgm_exp_name = drc[i]["bgm_exp_name"].ToString();
                        info.bgm_exp_descr = drc[i]["bgm_exp_descr"].ToString();
                        info.upper_bgm_exp_seq = cn.ConvertNullInt(drc[i]["upper_bgm_exp_seq"]);
                        info.bgm_exp_grp_seq = cn.ConvertNullInt(drc[i]["bgm_exp_grp_seq"]);
                        info.bgm_exp_grp_name = drc[i]["bgm_exp_grp_name"].ToString();
                        info.child_count = cn.ConvertNullInt(drc[i]["child_count"]);
                        info.record_status = drc[i]["record_status"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<M_BGM_MAS_Expense> GetList(Nullable<int> bgm_exp_grp_seq, Nullable<int> bgm_source_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_BGM_MAS_Expense> infoList = new List<M_BGM_MAS_Expense>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@bgm_exp_grp_seq", bgm_exp_grp_seq);
                dbManager.AddParameters(2, "@bgm_source_seq", bgm_source_seq);

                strSQL = "SELECT exp.bgm_exp_seq, exp.bgm_exp_name"
                    + " FROM bgm_mas_expense exp"
                    + " INNER JOIN bgm_mas_expense_source exps ON exp.bgm_exp_seq = exps.bgm_exp_seq"
                        + " AND exps.bgm_source_seq = @bgm_source_seq"
                        + " AND exps.record_status not like @delete_status"
                    + " LEFT JOIN (SELECT upper_bgm_exp_seq, count(*) as child_count"
                        + " FROM bgm_mas_expense"
                        + " WHERE record_status not like @delete_status"
                        + " GROUP BY upper_bgm_exp_seq"
                        + ") child on exp.bgm_exp_seq = child.upper_bgm_exp_seq"
                    + " WHERE exp.record_status not like @delete_status"
                    + (bgm_exp_grp_seq != null ? " and exp.bgm_exp_grp_seq = @bgm_exp_grp_seq" : "")
                    + " and coalesce(child.child_count, 0) = 0";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_BGM_MAS_Expense info = new M_BGM_MAS_Expense();
                        info.bgm_exp_seq = Convert.ToInt32(drc[i]["bgm_exp_seq"]);
                        info.bgm_exp_name = drc[i]["bgm_exp_name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<M_BGM_MAS_Expense> GetGroupChildList(Nullable<int> bgm_exp_grp_seq, Nullable<int> bgm_source_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_BGM_MAS_Expense> infoList = new List<M_BGM_MAS_Expense>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@bgm_exp_grp_seq", bgm_exp_grp_seq);
                dbManager.AddParameters(2, "@bgm_source_seq", bgm_source_seq);

                strSQL = "SELECT exp.bgm_exp_seq, exp.bgm_exp_name, coalesce(child.child_count, 0) as child_count"
                     + " FROM bgm_mas_expense exp"
                     + " INNER JOIN bgm_mas_expense_source exps ON exp.bgm_exp_seq = exps.bgm_exp_seq"
                        + " AND exps.bgm_source_seq = @bgm_source_seq"
                        + " AND exps.record_status not like @delete_status"
                     + " LEFT JOIN (SELECT upper_bgm_exp_seq, count(*) as child_count"
                        + " FROM bgm_mas_expense"
                        + " WHERE record_status not like @delete_status"
                        + " GROUP BY upper_bgm_exp_seq"
                        + ") child on exp.bgm_exp_seq = child.upper_bgm_exp_seq"
                     + " WHERE exp.record_status not like @delete_status"
                     + " and exp.upper_bgm_exp_seq is null"
                     + (bgm_exp_grp_seq != null ? " and exp.bgm_exp_grp_seq = @bgm_exp_grp_seq" : "");
                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;

                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_BGM_MAS_Expense info = new M_BGM_MAS_Expense();
                        info.bgm_exp_seq = Convert.ToInt32(drc[i]["bgm_exp_seq"]);
                        info.bgm_exp_name = drc[i]["bgm_exp_name"].ToString();
                        info.child_count = cn.ConvertNullInt(drc[i]["child_count"]);
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<M_BGM_MAS_Expense> GetChildList(Nullable<int> bgm_exp_seq, Nullable<int> bgm_source_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_BGM_MAS_Expense> infoList = new List<M_BGM_MAS_Expense>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@bgm_exp_seq", bgm_exp_seq);
                dbManager.AddParameters(2, "@bgm_source_seq", bgm_source_seq);

                strSQL = "SELECT exp.bgm_exp_seq, exp.bgm_exp_name, coalesce(child.child_count, 0) as child_count"
                    + " FROM bgm_mas_expense exp"
                    + " LEFT JOIN bgm_mas_expense_source exps ON exp.bgm_exp_seq = exps.bgm_exp_seq"
                        + " AND exps.bgm_source_seq = @bgm_source_seq"
                        + " AND exps.record_status not like @delete_status"
                    + " LEFT JOIN (SELECT upper_bgm_exp_seq, count(*) as child_count"
                        + " FROM bgm_mas_expense"
                        + " WHERE record_status not like @delete_status"
                        + " GROUP BY upper_bgm_exp_seq"
                        + ") child on exp.bgm_exp_seq = child.upper_bgm_exp_seq"
                    + " WHERE exp.record_status not like @delete_status"
                    + (bgm_exp_seq != null ? " and exp.upper_bgm_exp_seq = @bgm_exp_seq" : "");
                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;

                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_BGM_MAS_Expense info = new M_BGM_MAS_Expense();
                        info.bgm_exp_seq = Convert.ToInt32(drc[i]["bgm_exp_seq"]);
                        info.bgm_exp_name = drc[i]["bgm_exp_name"].ToString();
                        info.child_count = cn.ConvertNullInt(drc[i]["child_count"]);
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public M_BGM_MAS_Expense GetInfo(int seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_BGM_MAS_Expense info = new M_BGM_MAS_Expense();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@seq", seq);

                strSQL = "SELECT exp.bgm_exp_seq, exp.bgm_exp_code, exp.bgm_exp_name"
                    + ", exp.bgm_exp_descr, exp.upper_bgm_exp_seq"
                    + ", exp.bgm_exp_grp_seq, expg.bgm_exp_grp_name"
                    + ", exp.record_status, coalesce(child.child_count, 0) as child_count"
                    + " FROM bgm_mas_expense exp"
                    + " LEFT JOIN bgm_mas_expense_group expg ON exp.bgm_exp_grp_seq = expg.bgm_exp_grp_seq"
                    + " LEFT JOIN (SELECT upper_bgm_exp_seq, count(*) as child_count"
                        + " FROM bgm_mas_expense"
                        + " WHERE record_status not like 'D'"
                        + " GROUP BY upper_bgm_exp_seq"
                        + ") child on exp.bgm_exp_seq = child.upper_bgm_exp_seq"
                    + " WHERE exp.bgm_exp_seq = @seq";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.bgm_exp_seq = Convert.ToInt32(drc[0]["bgm_exp_seq"]);
                    info.bgm_exp_code = drc[0]["bgm_exp_code"].ToString();
                    info.bgm_exp_name = drc[0]["bgm_exp_name"].ToString();
                    info.bgm_exp_descr = drc[0]["bgm_exp_descr"].ToString();
                    info.upper_bgm_exp_seq = cn.ConvertNullInt(drc[0]["upper_bgm_exp_seq"]);
                    info.bgm_exp_grp_seq = cn.ConvertNullInt(drc[0]["bgm_exp_grp_seq"]);
                    info.bgm_exp_grp_name = drc[0]["bgm_exp_grp_name"].ToString();
                    info.child_count = cn.ConvertNullInt(drc[0]["child_count"]);
                    info.record_status = drc[0]["record_status"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }
    }
}
